package com.climbingGuide.java;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.markupartist.android.widget.ActionBar;

public class CapaSectores extends ItemizedOverlay<SectorItem>{
	private ArrayList<SectorItem> sectores = new ArrayList<SectorItem>();
	private Context contexto;
	private ActionBar barra;
	private SectorItem lastItemTapped;
	private MapView mapa;

	public CapaSectores(Drawable marker, ArrayList<SectorItem> _sectores, Context context, ActionBar _barra, MapView _mapa) {
		super(boundCenterBottom(marker));
		sectores = _sectores;
		contexto = context;
		barra = _barra;
		mapa = _mapa;
		populate();
	}

	@Override
	protected SectorItem createItem(int i) {
		return sectores.get(i);
	}

	@Override
	public int size() {
		return sectores.size();
	}
	@Override
	protected boolean onTap(int index) {
		SectorItem sector = sectores.get(index);
		
		if (lastItemTapped == sector)
			lastItemTapped = null;
		
		sector.setActivo(!sector.esActivo());
		if (lastItemTapped != null)
			lastItemTapped.setActivo(false);
		lastItemTapped = sector;
		
		if (sector.esActivo()){
			barra.setTitle("" + sector.getTitle()+ " |" + sector.getNumVias() + " vias"+ " |" + sector.getSnippet());
			mapa.getController().animateTo(sector.getPoint());
		}else{
			barra.setTitle("");
		}
		return true;
	}
}
